package gui;

import houseHold.Room;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.SystemColor;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import sensor.Sensor;
import sensor.SensorType;
import RoomPanel.IDGenerator;

public class AddSensor extends JDialog {

	private static final long serialVersionUID = 1L;
	private JButton AddButton = null;
	private JPanel BorderPanel = null;
	private Room callingRoom; // @jve:decl-index=0:
	private JButton CancelButton = null;
	private JLabel ErrorLabel = null;
	private JPanel jContentPane = null;
	private RoomInformationDialog jd;
	private JTextField LocationField = null;
	private JLabel LocationLabel = null;
	private JTextField NameField = null;
	private JLabel NameLabel = null;
	private Sensor s = null;
	private JComboBox TypeBox = null;
	private JLabel TypeLabel = null;

	/**
	 * @param owner
	 */
	public AddSensor(Frame owner, RoomInformationDialog JD) {
		super(owner);
		jd = JD;

	}

	/**
	 * This method initializes AddButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getAddButton() {
		if (AddButton == null) {
			AddButton = new JButton();
			AddButton.setBounds(new Rectangle(10, 210, 110, 25));
			AddButton.setToolTipText("Click to add Sensor");
			AddButton.setText("Add");
			AddButton.setEnabled(false);
			AddButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					s = new Sensor();
					s.setType((SensorType) TypeBox.getSelectedItem());
					callingRoom.addSensor(s);
					jd.setEnabled(true);
					jd.populateLists();
					setVisible(false);

				}
			});
		}
		return AddButton;
	}

	/**
	 * This method initializes BorderPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getBorderPanel() {
		if (BorderPanel == null) {
			ErrorLabel = new JLabel();
			ErrorLabel.setBounds(new Rectangle(25, 145, 210, 25));
			ErrorLabel.setForeground(new Color(196, 0, 0));
			ErrorLabel.setText("");
			LocationLabel = new JLabel();
			LocationLabel.setBounds(new Rectangle(10, 70, 60, 25));
			LocationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			LocationLabel.setText("Location: ");
			TypeLabel = new JLabel();
			TypeLabel.setBounds(new Rectangle(20, 110, 50, 25));
			TypeLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			TypeLabel.setText("Type: ");
			NameLabel = new JLabel();
			NameLabel.setBounds(new Rectangle(20, 30, 50, 25));
			NameLabel.setHorizontalAlignment(SwingConstants.TRAILING);
			NameLabel.setText("Name: ");
			BorderPanel = new JPanel();
			BorderPanel.setLayout(null);
			BorderPanel.setBounds(new Rectangle(10, 10, 255, 190));
			BorderPanel.setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createBevelBorder(BevelBorder.RAISED),
					BorderFactory.createTitledBorder(BorderFactory
							.createLineBorder(Color.gray, 5), "Add Sensor",
							TitledBorder.DEFAULT_JUSTIFICATION,
							TitledBorder.DEFAULT_POSITION, new Font(
									"SansSerif", Font.BOLD, 14),
							SystemColor.textHighlight)));
			BorderPanel.add(NameLabel, null);
			BorderPanel.add(TypeLabel, null);
			BorderPanel.add(LocationLabel, null);
			BorderPanel.add(ErrorLabel, null);
			BorderPanel.add(getNameField(), null);
			BorderPanel.add(getLocationFiel(), null);
			BorderPanel.add(getTypeBox(), null);

		}
		return BorderPanel;
	}

	/**
	 * This method initializes CancelButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getCancelButton() {
		if (CancelButton == null) {
			CancelButton = new JButton();
			CancelButton.setBounds(new Rectangle(155, 210, 110, 25));
			CancelButton.setText("Cancel");
			CancelButton.setToolTipText("Click to cancel");
			CancelButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jd.setEnabled(true);
					setVisible(false);
				}
			});

		}
		return CancelButton;
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getAddButton(), null);
			jContentPane.add(getCancelButton(), null);
			jContentPane.add(getBorderPanel(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes LocationFiel
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getLocationFiel() {
		if (LocationField == null) {
			LocationField = new JTextField();
			LocationField.setBounds(new Rectangle(80, 70, 150, 25));
			LocationField.setBackground(Color.white);
			LocationField.setEditable(false);
		}
		return LocationField;
	}

	/**
	 * This method initializes NameField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getNameField() {
		if (NameField == null) {
			NameField = new JTextField();
			NameField.setBounds(new Rectangle(80, 30, 150, 25));
			NameField.setBackground(Color.white);
			NameField.setEditable(false);
		}
		return NameField;
	}

	/**
	 * This method initializes TypeBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	private JComboBox getTypeBox() {
		if (TypeBox == null) {
			TypeBox = new JComboBox(SensorType.toDialogList());
			TypeBox.setBounds(new Rectangle(80, 110, 150, 25));
			TypeBox.setBackground(new Color(250, 250, 250));
			TypeBox.setSelectedIndex(-1);
			TypeBox.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					NameField.setText(TypeBox.getSelectedItem() + "#"
							+ IDGenerator.getNextSensorID());
					writeError("");
				}
			});
		}
		return TypeBox;
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	public void initialize(Room cRoom) {
		this.setBounds(new Rectangle(0, 0, 290, 280));
		this.setTitle("Add a new sensor");
		callingRoom = cRoom;
		this.setContentPane(getJContentPane());
		LocationField.setText(callingRoom.getName());
		writeError("Select Sensor type");
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			@Override
			public void windowClosing(java.awt.event.WindowEvent e) {
				jd.setEnabled(true);
			}
		});
	}

	private void writeError(String s) {
		if (s.length() > 0) {
			AddButton.setEnabled(false);
			ErrorLabel.setText(s);
		} else {
			AddButton.setEnabled(true);
			ErrorLabel.setText("");
		}
	}

} // @jve:decl-index=0:visual-constraint="10,10"
